const app = getApp();
Page({
  data: {
    xhx_images: app.xhx_images,
    xhx_route: app.xhx_route,
    customTags: [],
    pubTags: [],
    prevPage: null,
  },
  onLoad,
  done,
  getTags,
  tagClass,
  checkCustomTag,
  checkTag,
  comfirm,
})

function onLoad() {
  const that = this;
  that.getTags();
}

function tagClass() {
  const that = this;
  var customTags = [];
  var pubTags = [];
  var tags = that.data.tags;
  var prevPage = getCurrentPages()[getCurrentPages().length - 2] || {};

  for (var i = 0; i < tags.length; i++) {
    if (tags[i].operateFlag == 1) {
      customTags.push(tags[i])
    } else if (tags[i].operateFlag == 2) {
      pubTags.push(tags[i])
    }
  }

  if (prevPage.route) {
    var selCustomTags = prevPage.data.selCustomTags || [];
    if (selCustomTags.length > 0) {
      for (var i = 0; i < selCustomTags.length; i++) {
        for (var j = 0; j < customTags.length; j++) {
          if (selCustomTags[i].uuid == customTags[j].uuid) {
            customTags[j].status = selCustomTags[i].status;
            customTags[j].isHide = true;
          }
        }
      }
    }

    var selPubTags = prevPage.data.selPubTags || [];
    if (selPubTags.length > 0) {
      for (var i = 0; i < selPubTags.length; i++) {
        for (var j = 0; j < pubTags.length; j++) {
          if (selPubTags[i].uuid == pubTags[j].uuid) {
            pubTags[j].status = selPubTags[i].status;
            pubTags[j].isHide = true;
          }
        }
      }
    }
  }

  that.setData({
    customTags,
    pubTags,
    prevPage,//初始时获取上一个页面数据。置入data中。此页面无需再次获取
  })
}

function done(event) {
  const that = this;
  var value = event.detail.value;
  var customTags = that.data.customTags;

  // 区分输入框确定或者按钮确定
  if (event.type === "submit"){
    value = value.value;
  }

  if (!value) return;

  var reqData = {
    "labelName": value, //'标签名称',
    "remark": "", //备注
    "operateFlag": "1", //操作者类型（1代表前端用户，2代表后台用户）',
  }

  app.middleware.requestAjax({
    serAddr: app.serAddrs.labelAdd,
    data: reqData,
    success: function(res) {
      customTags.push({
        uuid: res.data,
        labelName: value,
        operateFlag: 1, //个人标签
        status: 1,
      })
      that.setData({
        customTags,
        value:""
      })
      that.comfirm();
    },
    fail:function(res){
      app.middleware.showToast(res && res.msg || "添加失败");
    }
  })
}

function getTags() {
  const that = this;

  app.middleware.requestAjax({
    serAddr: app.serAddrs.labelList,
    success: function(res) {
      that.setData({
        tags: res.data
      })
      that.tagClass();
    }
  })
}

function checkCustomTag(event) {
  const that = this;
  const index = event.currentTarget.dataset.index;
  var customTags = that.data.customTags;
  customTags[index].status = customTags[index].status ? 0 : 1;
  that.setData({
    customTags,
  })
  that.comfirm();
}

function checkTag(event) {
  const that = this;
  const index = event.currentTarget.dataset.index;
  var pubTags = that.data.pubTags;
  pubTags[index].status = pubTags[index].status ? 0 : 1;
  that.setData({
    pubTags,
  })
  that.comfirm();
}

function comfirm() {
  const that = this;
  var customTags = that.data.customTags;
  var pubTags = that.data.pubTags;
  var prevPage = that.data.prevPage;
  var selCustomTags = customTags.filter(key => key.status && !key.isHide);
  var selPubTags = pubTags.filter(key => key.status && !key.isHide);
  var form = prevPage.data.form;
  var prevCustomTags = prevPage.data.selCustomTags || [];
  var prevPubTags = prevPage.data.selPubTags || [];

  if (selCustomTags.length > 0) {
    selCustomTags.forEach((item) => {
      form.releaseContent += `#${item.labelName}#`;
      prevCustomTags.push(item);
    })
  }
  if (selPubTags.length > 0) {
    selPubTags.forEach((item) => {
      form.releaseContent += `#${item.labelName}#`;
      prevPubTags.push(item);
    })
  }

  prevPage.setData({
    form,
    selCustomTags: prevCustomTags,
    selPubTags: prevPubTags,
  })
  wx.navigateBack();
}